<script setup>
import { reactive, ref } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
const router = useRouter();
const store = useStore();

const role = ref("pat");

const formState = reactive({
  username: "",
  roomId: "",
  remember: true,
});

const onFinish = (values) => {
  console.log("Success:", values,role.value);
  store.commit("setRoomId", values.roomId);
  store.commit("setUser", values.username);
  if (role.value == "doc") {
    router.push({ name: "Doc" });
  } else if (role.value == "pat") {
    router.push({ name: "Pat" });
  }
};

const onFinishFailed = (errorInfo) => {
  console.log("Failed:", errorInfo);
};
</script>

<template>
  <div class="home">
    <div class="role--wrap">
      <a-radio-group v-model:value="role">
        <a-radio value="pat">发布端</a-radio>
        <a-radio value="doc">订阅端</a-radio>
      </a-radio-group>
    </div>
    <a-form
      :model="formState"
      name="basic"
      :label-col="{ span: 8 }"
      :wrapper-col="{ span: 16 }"
      autocomplete="off"
      @finish="onFinish"
      @finishFailed="onFinishFailed"
    >
      <a-form-item
        label="Username"
        name="username"
        :rules="[{ required: true, message: 'Please input your username!' }]"
      >
        <a-input v-model:value="formState.username" />
      </a-form-item>

      <a-form-item
        label="RoomId"
        name="roomId"
        :rules="[{ required: true, message: 'Please input your roomId!' }]"
      >
        <a-input v-model:value="formState.roomId" />
      </a-form-item>

      <a-form-item name="remember" :wrapper-col="{ offset: 8, span: 16 }">
        <a-checkbox v-model:checked="formState.remember"
          >Remember me</a-checkbox
        >
      </a-form-item>

      <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
        <a-button type="primary" html-type="submit">Submit</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<style scoped lang="scss">
.home {
  width: 700px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  .role--wrap{
    margin-bottom: 50px;
  }
}
</style>
